package top.itmeans.yes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import top.itmeans.yes.entity.HomeRecommendProduct;
import top.itmeans.yes.mapper.HomeRecommendProductMapper;
import top.itmeans.yes.service.HomeRecommendProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 人气推荐表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class HomeRecommendProductServiceImpl extends ServiceImpl<HomeRecommendProductMapper, HomeRecommendProduct> implements HomeRecommendProductService {

    @Resource
    private HomeRecommendProductMapper recommendProductMapper;
    @Override
    public int create(List<HomeRecommendProduct> homeRecommendProductList) {
        for (HomeRecommendProduct recommendProduct : homeRecommendProductList) {
            recommendProduct.setRecommendStatus(1);
            recommendProduct.setSort(0);
            recommendProductMapper.insert(recommendProduct);
        }
        return homeRecommendProductList.size();
    }

    @Override
    public int updateSort(Long id, Integer sort) {
        HomeRecommendProduct recommendProduct = new HomeRecommendProduct();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        return recommendProductMapper.updateById(recommendProduct);
    }

    @Override
    public int delete(List<Long> ids) {
        return recommendProductMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        LambdaQueryWrapper<HomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(HomeRecommendProduct::getId,ids);
        HomeRecommendProduct record = new HomeRecommendProduct();
        record.setRecommendStatus(recommendStatus);
        return recommendProductMapper.update(record,queryWrapper);
    }

    @Override
    public Page<HomeRecommendProduct> list(String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        Page<HomeRecommendProduct> homeRecommendProductPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(productName)){
            queryWrapper.like(HomeRecommendProduct::getProductName,productName);
        }
        if(recommendStatus!=null){
            queryWrapper.eq(HomeRecommendProduct::getRecommendStatus,recommendStatus);
        }
        queryWrapper.orderByDesc(HomeRecommendProduct::getSort);
        return recommendProductMapper.selectPage(homeRecommendProductPage,queryWrapper);
    }
}
